<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends Controller {
    // 后台登录页
    public function index(){
        if(isset($_SESSION['admin'])){
            //刷订单
            $order = D('order'); 
            $user = D('user');     
            $orderList = $order->order('create_at desc')->select();        
            for ($i=0; $i < count($orderList); $i++) { 
            $userId = $order->field('user_id')->order('create_at desc')->select();
            $userid = $userId[$i]['user_id'];
            $username = $user->field('username')->where("id='$userid'")->select(); 
            $orderList[$i]['username'] = $username[0]['username'];                     
                    $itemList = $orderList[$i]['orderitemlist'];
                    $orderItemList[$i]=explode(',',$itemList);
                    //dump(count($orderItemList[$i]));
                    $orderItem = D('orderitem');
                    $phone = D('phone');                 
                    for ($j = 0; $j < count($orderItemList[$i]); $j++) {                                                     
                            $orderItemId[$i] = $orderItemList[$i][$j];
                            $s[$i][$j] = $orderItem->where("id='$orderItemId[$i]'")->select();
                            $s1[$i][$j] = $s[$i][$j][0];
                            $phoneId = $orderItem->field('phone_id')->where("id='$orderItemId[$i]'")->select();
                            $phoneid = $phoneId[0]['phone_id'];
                            $phoneName = $phone->field('name')->where("id='$phoneid'")->select();
                            // $number = $phone->field('number')->where("id='$phoneid'")->select();
                            // $s1[$i][$j]['number'] = $number[0]['number'];                        
                            $s1[$i][$j]['phonename'] = $phoneName[0]['name'];                                                                           
                    }                  
                $orderList[$i]['item'] = $s1[$i];              
                        
            }   
            $this->assign('orderList',$orderList); 
            $this->display();
            
        }else{
            $url = 'http://localhost/book-order/index.php/Admin/Index/manage';
            header("location: $url");
        }
    }
 
 
//刷列表
    public function orderList(){
        $order = D('order');
        $resultArr = $order->select();
        //echo json_encode($resultArr,JSON_UNESCAPED_UNICODE);
        echo json_encode($result);
    }

}
